﻿using Infrastructure.Web;
using MyData;
using MyData.Data.Repository;
using MyData.Dto;
using MyData.Models;
using MyDomain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRepository
{
  public  class MeetRepository : RepositoryBase<Meet>, IMeetRepository
    {
        public PageDto<MeetDto> GetMeetList(QueryListModel query, string meetName)
        {
            using (var db = new RepositoryBase().BeginTrans())
            {
                var list = db.IQueryable<Meet>();
                if (string.IsNullOrEmpty(meetName))
                {
                    list = list.Where(c => c.MeetName.Contains(meetName));
                }
                var dtos = list.Select(c => new MeetDto()
                {
                    Id = c.Id,
                    MeetName = c.MeetName,
                    MeetRemark = c.MeetRemark,
                    CreatedTime = c.CreatedTime
                }).ToList();
                foreach (var meet in dtos)
                {
                    var users = db.IQueryable<MeetUser>().Where(c=>c.MeetId==meet.Id).Select(c=>c.Id);
                    var records = db.IQueryable<AccessRecord>().Where(c => c.MobileId == Guid.Empty && users.Contains(c.UserId)).Select(c=>c.UserId).Distinct();
                    meet.AllUser = users.Count();
                    meet.RealUser = records.Count();
                }
                var result = dtos.OrderByDescending(t => t.CreatedTime).AsQueryable().Paging(query.Limit, query.Offset);
                return new PageDto<MeetDto>()
                {
                    total = result.total,
                    rows = result.rows
                };
            }
        }
        public bool AddMeet(Meet meet)
        {
            using (var db = new RepositoryBase().BeginTrans())
            {
                if (db.FindEntity<Meet>(c => c.MeetName == meet.MeetName) != null)
                {
                    throw new KnownException("会议名称重复");
                }
                meet.Id = Guid.NewGuid();
                db.Insert<Meet>(meet);
                return db.Commit() > 0;
            }
        }
        public bool UpdateMeet(Meet meet)
        {
            using (var db = new RepositoryBase().BeginTrans())
            {
                if (db.FindEntity<Meet>(c => c.MeetName == meet.MeetName&&c.Id!=meet.Id) != null)
                {
                    throw new KnownException("会议名称重复");
                }
                var model = db.FindEntity<Meet>(meet.Id);
                model.MeetName = meet.MeetName;
                model.MeetRemark = meet.MeetRemark;
                db.Update<Meet>(model);
                return db.Commit() > 0;
            }
        }
        public bool DeleteMeet(Guid id)
        {
            using (var db = new RepositoryBase().BeginTrans())
            {
                var model = db.FindEntity<Meet>(id);
                db.Delete<Meet>(model);
                return db.Commit() > 0;
            }
        }
        public MeetDto GetMeetDto(Guid? id)
        {
            using (var db = new RepositoryBase().BeginTrans())
            {
                var list = db.IQueryable<Meet>().OrderByDescending(c=>c.CreatedTime);
                var dtos = list.Select(c => new MeetDto()
                {
                    Id = c.Id,
                    MeetName = c.MeetName,
                    MeetRemark = c.MeetRemark,
                    CreatedTime = c.CreatedTime
                });
                if (id.HasValue)
                {
                    return dtos.FirstOrDefault(c => c.Id == id.Value);
                }
                else
                {
                    return dtos.FirstOrDefault();
                }

            }
        }
        public bool ClearMeet(Guid id)
        {
            using (var db = new RepositoryBase().BeginTrans())
            {
                var meetId = db.IQueryable<Meet>().OrderByDescending(c => c.CreatedTime).FirstOrDefault().Id;
                var users = db.IQueryable<MeetUser>().Where(c => c.MeetId == meetId);
                var mobiles = db.IQueryable<Mobile>();
                foreach (var mobile in mobiles)
                {
                    foreach (var user in users)
                    {
                        var syns = new SyncRL() { Id = Guid.NewGuid(), UserId = user.Id, MobileId = mobile.Id, OperateType = (int)EnumOperateType.删除 };
                        db.Insert(syns);
                    }
                }
                return true;

            }
        }
    }
}
